<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class RestaurantsController extends JController
{
	function comment()
	{
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'tables');

		$row =& JTable::getInstance('comment', 'Table');

		$row->review_id = JRequest::getInt('review_id', 0);

		$row->comment_date = date( 'Y-m-d H:i:s' );

		$user =& JFactory::getUser();

		if($user->id)
		{
			$row->user_id = $user->id;
		}

		$row->full_name = JRequest::getString('full_name', '');
		$row->comment_text = JRequest::getString('comment_text', '');

		if (!$row->store())
		{
			JError::raiseError(500, $row->getError());
		}

		$this->setRedirect(JRoute::_('index.php?option=com_restaurants&id=' . $row->review_id . '&view=single'), 'Comment Added.');
	}
	
	function display()
	{
		$view = JRequest::getVar('view');
		
		if (!$view) {
			JRequest::setVar('view', 'list');
		}
		
		parent::display();
	}
	
	function sendemail()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );

		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'tables');

		$sender_email = JRequest::getString('sender_email', '');
		$recipient = JRequest::getString('recipient', '');
		$sender_name = JRequest::getString('sender_name', '');
		$message = JRequest::getString('message', '');
		$id = JRequest::getInt('id', 0);

		jimport( 'joomla.mail.helper' );

		if (!JMailHelper::isEmailAddress($sender_email) || !JMailHelper::isEmailAddress($recipient)) {
			JError::raiseError(500, 'One of the emails you entered is invalid. Please try again.');
		}

		$review =& JTable::getInstance('review', 'Table');
		$review->load($id);

		$link = JURI::base() . 'index.php?option=com_restaurants&view=single&id=' . $id;

		$subject = $sender_name . ' wants you to know about ' . $review->name;

		$body = "Here's a review of {$review->name}:\n\n";
		$body .= "{$review->review}\n\n";

		if ($message != '') {
			$body .= $sender_name . " also added this message:\n";
			$body .= '"' . $message . '"' . "\n\n";
		}

		$body .= "For all of the details, follow this link: {$link}";

		$sender_name = JMailHelper::cleanAddress($sender_name);
		$subject = JMailHelper::cleanSubject($subject);
		$body = JMailHelper::cleanBody($body);

		if (JUtility::sendMail($sender_email, $sender_name, $recipient, $subject, $body) !== true)
		{
			JError::raiseNotice( 500, 'Email failed.' );
		}

	 	JRequest::setVar('view', 'email');
		JRequest::setVar('layout', 'success');

		$this->display();
	}
}

$controller = new RestaurantsController();
$controller->execute( $task );
$controller->redirect(); 
